
import axios from 'axios'
import qs from 'qs'
const http=axios.create({
  timeout:10000, //设置请求超时时间ms
  withCredentials : true , //表示跨域请求 false  不跨域  true 跨域
})
//http.defaults.timeout = 10000 //设置请求超时时间
//http.defaults.withCredentials = true // 表示跨域请求时是否需要使用凭证 false  不跨域  true 跨域
//http.defaults.headers.common['token'] = localstorage.getItem('token'); //设置请求头，这个是不是必须的
console.log(11,http)
//处理post 请求头
if (http.method === 'post') {   //post请求
  if (http.headerType === "json") {
      axios.defaults.headers["Content-Type"] = 'application/json;charset=UTF-8'
      data = http.params
  } else {
      http.defaults.headers["Content-Type"] = 'application/x-www-form-urlencoded;charset=UTF-8'
      data = qs.stringify(http.params);
  }
}

//get请求 
if (http.method === 'get') {
  console.log('get http-->',http)
  if (http.headerType == !'json') {
      http.defaults.headers["Content-Type"] = 'application/x-www-form-urlencoded;charset=UTF-8'
  }
  data = qs.stringify(http.params);
  path = http.path + '?' + http.data
  data = {};
}
//request请求拦截
http.interceptors.request.use(
  res=>{
    console.log(36,res)
  },
  err=>{
    console.log(39,err)
  }
)
// respone响应拦截器
http.interceptors.response.use(
  response => {    
    console.log(45,response)
    const res = response;
    return res
     
  },
  err => { 
    console.log(50,err)
    // if (err && err.response) {
    //   switch (err.response.status) {
    //     case 400:
    //       console.log('400')
    //       break;
    //     case 401:
    //        console.log('401')  
    //       break;
    //     case 403:
    //        err.response.data.message 
    //       break;
    //     case 404:
    //        console.log('404')
    //       break;
    //     case 405:
    //        console.log('405')
    //       break;
    //     case 408:
    //        console.log('408')
    //       break;
    //     case 500:
    //        console.log('500')
    //       break;
    //     case 501:
    //        console.log('501')
    //       break;
    //     case 502:
    //        console.log('502')
    //       break;
    //     case 503:
    //        console.log('503')
    //       break;
    //     case 504:
    //        console.log('504')
    //       break;
    //     case 505:
    //        console.log('505')
    //       break;
    //     default:
    //        console.log('Default') + `${err.response.status}`
    //   }
    // } 
    return Promise.reject(err)
  }
)
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params){    
  return new Promise((resolve, reject) =>{        
      axios.get(url, {            
          params: params        
      }).then(res => {
          resolve(res.data);
      }).catch(err =>{
          reject(err.data)        
  })    
});}

/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params) {
  return new Promise((resolve, reject) => {
       axios.post(url, qs.stringify(params))
      .then(res => {
          resolve(res.data);
      })
      .catch(err =>{
          reject(err.data)
      })
  });
}
/**
 * path:
 * method:
 * params:
 */
// export default http